package com.zhidisoft.system.vo;

import org.apache.commons.lang3.StringUtils;

import com.zhidisoft.system.entity.Function;
import com.zhidisoft.system.entity.Role;
import com.zhidisoft.system.entity.User;
import com.zhidisoft.util.DateUtil;

public class RoleVO {
	
	private String id;
	
	private String roleName;
	
	private String roleNote;
	
	private Integer sortNum;
	
	private Integer status;
	
	private String funcIds;
	
	private String createById;
	
	private String createBy;
	
	private String createTime;
	
	private String updateById;
	
	private String updateBy;
	
	private String updateTime;

	public String getId() {
		return id;
	}

	public String getRoleName() {
		return roleName;
	}

	public String getRoleNote() {
		return roleNote;
	}

	public Integer getSortNum() {
		return sortNum;
	}

	public Integer getStatus() {
		return status;
	}

	public String getFuncIds() {
		return funcIds;
	}

	public String getCreateById() {
		return createById;
	}

	public String getCreateBy() {
		return createBy;
	}

	public String getCreateTime() {
		return createTime;
	}

	public String getUpdateById() {
		return updateById;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public void setRoleNote(String roleNote) {
		this.roleNote = roleNote;
	}

	public void setSortNum(Integer sortNum) {
		this.sortNum = sortNum;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public void setFuncIds(String funcIds) {
		this.funcIds = funcIds;
	}

	public void setCreateById(String createById) {
		this.createById = createById;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public void setUpdateById(String updateById) {
		this.updateById = updateById;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}
	
	/**
	 * 实体转换为VO
	 * @param role
	 * @return
	 */
	public static RoleVO fromRole(Role role){
		if(role!=null){
			RoleVO vo = new RoleVO();
			vo.setId(role.getId());
			vo.setRoleName(role.getRoleName());
			vo.setRoleNote(role.getRoleNote());
			vo.setStatus(role.getStatus());
			vo.setSortNum(role.getSortNum());
			vo.setCreateTime( DateUtil.formatDate(role.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			vo.setUpdateTime( DateUtil.formatDate(role.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			
			if(role.getCreateUser()!=null){
				vo.setCreateBy(role.getCreateUser().getUsername());
				vo.setCreateById(role.getCreateUser().getId());
			}
			
			if(role.getUpdateUser()!=null){
				vo.setUpdateBy(role.getUpdateUser().getUsername());
				vo.setUpdateById(role.getUpdateUser().getId());
			}
			
			if(role.getFunctions()!=null && !role.getFunctions().isEmpty()){
				StringBuffer sb = new StringBuffer();
				for(Function func : role.getFunctions()){
					sb.append(func.getId());
					sb.append(",");
				}
				sb.deleteCharAt(sb.length()-1);
				vo.setFuncIds(sb.toString());
			}
			
			return vo;
		}
		return null;
	}
	
	public static Role toRole(RoleVO vo){
		if(vo!=null){
			Role role = new Role();
			if(StringUtils.isNotBlank(vo.getId())){
				role.setId(vo.getId());
			}
			role.setRoleName(vo.getRoleName());
			role.setRoleNote(vo.getRoleNote());
			role.setStatus(vo.getStatus());
			role.setSortNum(vo.getSortNum());
			role.setCreateTime( DateUtil.parseDate(vo.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			role.setUpdateTime( DateUtil.parseDate(vo.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			
			if(StringUtils.isNotBlank(vo.getCreateById())){
				User user = new User();
				user.setId(vo.getCreateById());
				role.setCreateUser(user);
			}
			
			if(StringUtils.isNotBlank(vo.getUpdateById())){
				User user = new User();
				user.setId(vo.getUpdateById());
				role.setUpdateUser(user);
			}
			
			if(StringUtils.isNotBlank(vo.getFuncIds())){
				String[] funcIds = vo.getFuncIds().split(",");
				for(String funcId : funcIds){
					Function func = new Function();
					func.setId(funcId);
					role.getFunctions().add(func);
				}
			}
			
			return role;
		}
		return null;
	}
}
